const utils = {
  compare: (property, desc = "asc") => {
    return function (a, b) {
      let value1 = a[property],
        value2 = b[property];
      if (desc === "desc") {
        //降序
        return value2 - value1;
      } else {
        //升序
        return value1 - value2;
      }
    };
  },
  buildTree: (arr) => {
    let tree = {};
    let list = arr.sort(utils.compare("parent_id"));
    list.map((ele, index) => {
      let parent = tree[ele.parent_id];
      if (parent) {
        if (!parent.children) {
          parent.children = [];
        }
        parent.children.push(ele);
      } else {
        tree[ele.id] = ele;
      }
    });
    let newArr = [];
    for (const key in tree) {
      newArr.push(tree[key]);
    }
    return newArr;
  },
};
export default utils;
